//
//  FlowCurrencyDetailController.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/11/12.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "FlowCurrencyDetailController.h"
#import "IntegralContentTableViewCell.h"
#import "IntergralRecordModel.h"

@interface FlowCurrencyDetailController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *detailTable;
@property (nonatomic, strong) NSMutableArray *mutArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation FlowCurrencyDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WhiteColor;
    self.page = 1;
    [self.view addSubview:self.detailTable];
    [self.detailTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.mas_equalTo(0);
    }];
    
    WS(weakSelf);
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf initRequese:YES];
    }];
    [header beginRefreshing];
    [header setTitle:@"下拉可以刷新!" forState:MJRefreshStateIdle];
    [header setTitle:@"松手立即刷新!" forState:MJRefreshStatePulling];
    [header setTitle:@"正在加载..." forState:MJRefreshStateRefreshing];
    header.stateLabel.font = Font(24);
    header.backgroundColor = [UIColor clearColor];
    [header setTintColor:[UIColor clearColor]];
    
    header.stateLabel.textColor = Color999999;
    header.lastUpdatedTimeLabel.hidden = YES;
    self.detailTable.mj_header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf initRequese:NO];
    }];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    [footer setTitle:@"正在加载。。。。" forState:MJRefreshStateRefreshing];
    [footer setTitle:@"没有更多数据！" forState:MJRefreshStateNoMoreData];
    footer.stateLabel.font = Font(17);
    footer.stateLabel.textColor = Color999999;
    self.detailTable.mj_footer = footer;
    self.detailTable.mj_footer.hidden = NO;
    
}


- (void)initRequese:(BOOL)isUp {
    WS(weakSelf);
    NSString *par = [NSString stringWithFormat:@"{\"user_id\" : \"%@\", \"type\" : \"%@\", \"page\" : \"%@\", \"number\" : \"%@\"}", [RSA encryptString:[CommonUserInfo sharedInstance].user_id publicKey:PublicKey], [RSA encryptString:[NSString stringWithFormat:@"%ld",(long)self.indexType] publicKey:PublicKey], [RSA encryptString:[NSString stringWithFormat:@"%ld",(long)self.page] publicKey:PublicKey], [RSA encryptString:@"10" publicKey:PublicKey]];
    
    [PPNetworkHelper POST:@"user/Bean/Beandeatil" parameters:@{@"json":par} success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue]==1) {
            if (isUp) {
                [self.mutArray removeAllObjects];
            }
            NSArray *array = responseObject[@"data"];
            for (NSDictionary *dic in array) {
                IntergralRecordModel *model = [IntergralRecordModel mj_objectWithKeyValues:dic];
                [weakSelf.mutArray addObject:model];
            }
        }
        [weakSelf.detailTable reloadData];
        
        
        [weakSelf.detailTable.mj_footer endRefreshing];
        [weakSelf.detailTable.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [weakSelf.detailTable.mj_footer endRefreshing];
        [weakSelf.detailTable.mj_header endRefreshing];
    }];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    IntegralContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IntegralContentTableViewCellDetail"];
    cell.type = self.indexType;
    cell.integralModel = self.mutArray[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.mutArray.count;
}

- (NSMutableArray *)mutArray {
    if (!_mutArray) {
        _mutArray = [[NSMutableArray alloc] init];
    }
    return _mutArray;
}

- (UITableView *)detailTable {
    if (!_detailTable) {
        _detailTable = [[UITableView alloc] init];
        _detailTable.delegate = self;
        _detailTable.dataSource = self;
        [_detailTable registerClass:[IntegralContentTableViewCell class] forCellReuseIdentifier:@"IntegralContentTableViewCellDetail"];
        _detailTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _detailTable.showsVerticalScrollIndicator = NO;
        _detailTable.showsHorizontalScrollIndicator = NO;
        _detailTable.estimatedSectionHeaderHeight = 0;
        _detailTable.estimatedSectionFooterHeight = 0;
        _detailTable.estimatedRowHeight = 0;
        if (@available(iOS 11.0, *)) {
            _detailTable.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _detailTable.rowHeight = jisuan(140);
    }
    return _detailTable;
}

@end
